package com.example.sth0409.tablayoutdemo;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        stringListaaa = new ArrayList<>();
        stringListaaa.add("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png");
        stringListaaa.add("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png");
        stringListaaa.add("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png");
        stringListaaa.add("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png");
        stringListaaa.add("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png");
        stringListaaa.add("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png");
        stringListaaa.add("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png");
        stringListaaa.add("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png");
        SampleFragmentPagerAdapter pagerAdapter =
                new SampleFragmentPagerAdapter(getSupportFragmentManager(), this);
        pagerAdapter.setTabTitles(stringListaaa);
        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(pagerAdapter.getTabView(i));
            }
        }

//        viewPager.setCurrentItem(1);
        tabLayout.getTabAt(0).getCustomView().setSelected(true);
    }


    public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
        int PAGE_COUNT = 0;
        private String tabTitles[];
        private Context context;

        public void setTabTitles(List<String> tabTitleslist) {
            int size = tabTitleslist.size();
            tabTitles = new String[size];
            PAGE_COUNT = size;
            for (int i = 0; i < tabTitleslist.size(); i++) {
                tabTitles[i] = tabTitleslist.get(i);
            }
        }

        public View getTabView(int position) {
            View v = LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
            TextView tv = (TextView) v.findViewById(R.id.news_title);
            tv.setText(tabTitles[position]);
            // ImageView img = (ImageView) v.findViewById(R.id.imageView);
            //img.setImageResource(imageResId[position]);
            return v;
        }

        public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
            super(fm);
            this.context = context;
        }

        @Override
        public int getCount() {
            return PAGE_COUNT;
        }

        @Override
        public Fragment getItem(int position) {

            return PageFragment.newInstance(position + 1);
        }

        @Override
        public CharSequence getPageTitle(int position) {

            return tabTitles[position];
        }
    }


    public static class PageFragment extends Fragment {
        public static final String ARG_PAGE = "ARG_PAGE";

        private int mPage;

        public static PageFragment newInstance(int page) {
            Bundle args = new Bundle();
            args.putInt(ARG_PAGE, page);
            PageFragment fragment = new PageFragment();
            fragment.setArguments(args);

            return fragment;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mPage = getArguments().getInt(ARG_PAGE);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_page, container, false);
            RecyclerView recyclerView = (RecyclerView) view;
//            textView.setText("Fragment #" + mPage);
            final List<String> strings = new ArrayList<>();
            recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
            MyAdapter myAdapter = new MyAdapter(getContext());
            myAdapter.setOnItemImgClickListener(new MyAdapter.OnItemImgClickListener() {
                @Override
                public void onItemImgClick(View v, int positon) {
                    String s = strings.get(positon);
                    StringBean stringBean=new StringBean(strings);
                    startActivity(new Intent(getContext(),ImageShowActivity.class).putExtra("bean",stringBean).putExtra("position",positon));
                }
            });
            recyclerView.setAdapter(myAdapter);

            if (mPage == 1) {
                strings.clear();
                strings.addAll(((MainActivity) getActivity()).stringListaaa);
            } else {
                strings.clear();
                strings.addAll(((MainActivity) getActivity()).stringListaaa.subList(0, mPage));
            }
            myAdapter.setStringList(strings);
            return view;
        }
    }

    public List<String> stringListaaa;

    static class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
        private List<String> stringList;
        private Context context;

        public interface OnItemImgClickListener {
            void onItemImgClick(View v, int positon);
        }

        private OnItemImgClickListener onItemImgClickListener;

        public void setOnItemImgClickListener(OnItemImgClickListener listener) {
            onItemImgClickListener = listener;
        }

        public List<String> getStringList() {
            return stringList;
        }

        public MyAdapter(Context context) {
            this.context = context;
            stringList = new ArrayList<>();
        }

        public void setStringList(List<String> stringList) {
            this.stringList = stringList;
            notifyDataSetChanged();
        }

        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.frag_item, parent, false);
            return new MyHolder(view);
        }

        @Override
        public void onBindViewHolder(MyHolder holder, final int position) {
            String s = stringList.get(position);
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemImgClickListener.onItemImgClick(view, position);
                }
            });
            Glide.with(context).load(s).into(holder.imageView);
        }

        @Override
        public int getItemCount() {
            return stringList.size();
        }

        class MyHolder extends RecyclerView.ViewHolder {
            ImageView imageView;

            public MyHolder(View itemView) {
                super(itemView);
                imageView = (ImageView) itemView;
            }
        }
    }
}
